]> www.fi.muni.cz Git - evince.git/blob - backend/ps/ps-document.c
61f07ab789011c6e42a74fb85e9f639ba4a46da1
[evince.git] / backend / 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, Carlos Garcia Campos
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
27 #include <glib/gstdio.h>
28 #include <glib/gi18n.h>
29 #include <gdk/gdkpixbuf.h>
30 #include <string.h>
31 #include <stdlib.h>
32
33 #include "ps-document.h"
34 #include "ps-interpreter.h"
35 #include "ps.h"
36 #include "gstypes.h"
37 #include "gsdefaults.h"
38 #include "ev-file-exporter.h"
39 #include "ev-async-renderer.h"
40 #include "ev-document-thumbnails.h"
41 #include "ev-document-misc.h"
42
43 struct _PSDocument {
44         GObject object;
45
46         gchar *filename;
47         struct document *doc;
48         gboolean structured_doc;
49
50         PSInterpreter *gs;
51
52         /* Document Thumbnails */
53         PSInterpreter   *thumbs_gs;
54         GdkPixbuf       *thumbnail;
55         EvRenderContext *thumbs_rc;
56         GMutex          *thumbs_mutex;
57         GCond           *thumbs_cond;
58         
59         /* File exporter */
60         gint  *ps_export_pagelist;
61         gchar *ps_export_filename;
62 };
63
64 struct _PSDocumentClass {
65         GObjectClass parent_class;
66 }; 
67
68 static void     ps_document_document_iface_init            (EvDocumentIface           *iface);
69 static void     ps_document_file_exporter_iface_init       (EvFileExporterIface       *iface);
70 static void     ps_async_renderer_iface_init               (EvAsyncRendererIface      *iface);
71 static void     ps_document_document_thumbnails_iface_init (EvDocumentThumbnailsIface *iface);
72
73 static void     ps_interpreter_page_rendered               (PSInterpreter             *gs,
74                                                             GdkPixbuf                 *pixbuf,
75                                                             PSDocument                *ps_document);
76
77 G_DEFINE_TYPE_WITH_CODE (PSDocument, ps_document, G_TYPE_OBJECT,
78                          {
79                                  G_IMPLEMENT_INTERFACE (EV_TYPE_DOCUMENT,
80                                                         ps_document_document_iface_init);
81                                  G_IMPLEMENT_INTERFACE (EV_TYPE_DOCUMENT_THUMBNAILS,
82                                                         ps_document_document_thumbnails_iface_init);
83                                  G_IMPLEMENT_INTERFACE (EV_TYPE_FILE_EXPORTER,
84                                                         ps_document_file_exporter_iface_init);
85                                  G_IMPLEMENT_INTERFACE (EV_TYPE_ASYNC_RENDERER,
86                                                         ps_async_renderer_iface_init);
87                          });
88
89 /* PSDocument */
90 static void
91 ps_document_init (PSDocument *ps_document)
92 {
93 }
94
95 static void
96 ps_document_dispose (GObject *object)
97 {
98         PSDocument *ps_document = PS_DOCUMENT (object);
99
100         if (ps_document->gs) {
101                 g_object_unref (ps_document->gs);
102                 ps_document->gs = NULL;
103         }
104
105         if (ps_document->thumbs_gs) {
106                 g_object_unref (ps_document->thumbs_gs);
107                 ps_document->thumbs_gs = NULL;
108         }
109         
110         if (ps_document->filename) {
111                 g_free (ps_document->filename);
112                 ps_document->filename = NULL;
113         }
114
115         if (ps_document->doc) {
116                 psfree (ps_document->doc);
117                 ps_document->doc = NULL;
118         }
119
120         if (ps_document->thumbnail) {
121                 g_object_unref (ps_document->thumbnail);
122                 ps_document->thumbnail = NULL;
123         }
124
125         if (ps_document->thumbs_mutex) {
126                 g_mutex_free (ps_document->thumbs_mutex);
127                 ps_document->thumbs_mutex = NULL;
128         }
129         
130         if (ps_document->thumbs_cond) {
131                 g_cond_free (ps_document->thumbs_cond);
132                 ps_document->thumbs_cond = NULL;
133         }
134
135         if (ps_document->thumbs_rc) {
136                 g_object_unref (ps_document->thumbs_rc);
137                 ps_document->thumbs_rc = NULL;
138         }
139
140         G_OBJECT_CLASS (ps_document_parent_class)->dispose (object);
141 }
142
143 static void
144 ps_document_class_init (PSDocumentClass *klass)
145 {
146         GObjectClass *object_class;
147
148         object_class = G_OBJECT_CLASS (klass);
149
150         object_class->dispose = ps_document_dispose;
151 }
152
153 /* EvDocumentIface */
154 static gboolean
155 document_load (PSDocument *ps_document, const gchar *fname, GError **error)
156 {
157         FILE *fd;
158         
159         ps_document->filename = g_strdup (fname);
160
161         /*
162          * We need to make sure that the file is loadable/exists!
163          * otherwise we want to exit without loading new stuff...
164          */
165         if (!g_file_test (fname, G_FILE_TEST_IS_REGULAR)) {
166                 gchar *filename_dsp;
167
168                 filename_dsp = g_filename_display_name (fname);
169                 g_set_error (error,
170                              G_FILE_ERROR,
171                              G_FILE_ERROR_NOENT,
172                              _("Cannot open file “%s”."),
173                              filename_dsp);
174                 g_free (filename_dsp);
175                 
176                 return FALSE;
177         }
178
179         if ((fd = fopen (ps_document->filename, "r")) == NULL) {
180                 gchar *filename_dsp;
181
182                 filename_dsp = g_filename_display_name (fname);
183                 g_set_error (error,
184                              G_FILE_ERROR,
185                              G_FILE_ERROR_NOENT,
186                              _("Cannot open file “%s”."),
187                              filename_dsp);
188                 g_free (filename_dsp);
189                 
190                 return FALSE;
191         }
192         
193         /* we grab the vital statistics!!! */
194         ps_document->doc = psscan (fd, TRUE, ps_document->filename);
195         fclose (fd);
196         if (!ps_document->doc)
197                 return FALSE;
198
199         ps_document->structured_doc =
200                 ((!ps_document->doc->epsf && ps_document->doc->numpages > 0) ||
201                  (ps_document->doc->epsf && ps_document->doc->numpages > 1));
202
203         ps_document->gs = ps_interpreter_new (ps_document->filename,
204                                               ps_document->doc);
205         g_signal_connect (G_OBJECT (ps_document->gs), "page_rendered",
206                           G_CALLBACK (ps_interpreter_page_rendered),
207                           (gpointer) ps_document);
208         
209         return TRUE;
210 }
211
212 static gboolean
213 ps_document_load (EvDocument  *document,
214                   const char  *uri,
215                   GError     **error)
216 {
217         char *filename;
218         char *gs_path;
219         gboolean result;
220
221         filename = g_filename_from_uri (uri, NULL, error);
222         if (!filename)
223                 return FALSE;
224
225         gs_path = g_find_program_in_path ("gs");
226         if (!gs_path) {
227                 gchar *filename_dsp;
228                 
229                 filename_dsp = g_filename_display_name (filename);
230                 g_set_error (error,
231                              G_FILE_ERROR,
232                              G_FILE_ERROR_NOENT,
233                              _("Failed to load document “%s”. Ghostscript interpreter was not found in path"),
234                              filename);
235                 g_free (filename_dsp);
236                 g_free (filename);
237                 
238                 return FALSE;
239         }
240         g_free (gs_path);
241
242         result = document_load (PS_DOCUMENT (document), filename, error);
243         if (!result && !(*error)) {
244                 gchar *filename_dsp;
245                 
246                 filename_dsp = g_filename_display_name (filename);
247                 g_set_error (error,
248                              G_FILE_ERROR,
249                              G_FILE_ERROR_FAILED,
250                              _("Failed to load document “%s”"),
251                              filename_dsp);
252                 g_free (filename_dsp);
253         }
254         
255         g_free (filename);
256
257         return result;
258 }
259
260 static gboolean
261 save_document (PSDocument *document, const char *filename)
262 {
263         gboolean result = TRUE;
264         GtkGSDocSink *sink = gtk_gs_doc_sink_new ();
265         FILE *f, *src_file;
266         gchar *buf;
267
268         src_file = fopen (document->filename, "r");
269         if (src_file) {
270                 struct stat stat_rec;
271
272                 if (stat (document->filename, &stat_rec) == 0) {
273                         pscopy (src_file, sink, 0, stat_rec.st_size - 1);
274                 }
275
276                 fclose (src_file);
277         }
278         
279         buf = gtk_gs_doc_sink_get_buffer (sink);
280         if (buf == NULL) {
281                 return FALSE;
282         }
283         
284         f = fopen (filename, "w");
285         if (f) {
286                 fputs (buf, f);
287                 fclose (f);
288         } else {
289                 result = FALSE;
290         }
291
292         g_free (buf);
293         gtk_gs_doc_sink_free (sink);
294         g_free (sink);
295
296         return result;
297 }
298
299 static gboolean
300 save_page_list (PSDocument *document, int *page_list, const char *filename)
301 {
302         gboolean result = TRUE;
303         GtkGSDocSink *sink = gtk_gs_doc_sink_new ();
304         FILE *f;
305         gchar *buf;
306
307         pscopydoc (sink, document->filename, 
308                    document->doc, page_list);
309         
310         buf = gtk_gs_doc_sink_get_buffer (sink);
311         
312         f = fopen (filename, "w");
313         if (f) {
314                 fputs (buf, f);
315                 fclose (f);
316         } else {
317                 result = FALSE;
318         }
319
320         g_free (buf);
321         gtk_gs_doc_sink_free (sink);
322         g_free (sink);
323
324         return result;
325 }
326
327 static gboolean
328 ps_document_save (EvDocument  *document,
329                   const char  *uri,
330                   GError     **error)
331 {
332         PSDocument *ps = PS_DOCUMENT (document);
333         gboolean result;
334         char *filename;
335
336         filename = g_filename_from_uri (uri, NULL, error);
337         if (!filename)
338                 return FALSE;
339
340         result = save_document (ps, filename);
341
342         g_free (filename);
343
344         return result;
345 }
346
347 static int
348 ps_document_get_n_pages (EvDocument *document)
349 {
350         PSDocument *ps = PS_DOCUMENT (document);
351
352         if (!ps->filename || !ps->doc) {
353                 return -1;
354         }
355
356         return ps->structured_doc ? ps->doc->numpages : 1;
357 }
358
359 static gint
360 ps_document_get_page_rotation (PSDocument *ps_document,
361                                int         page)
362 {
363         gint rotation = GTK_GS_ORIENTATION_NONE;
364
365         g_assert (ps_document->doc != NULL);
366         
367         if (ps_document->structured_doc) {
368                 if (ps_document->doc->pages[page].orientation != GTK_GS_ORIENTATION_NONE)
369                         rotation = ps_document->doc->pages[page].orientation;
370                 else
371                         rotation = ps_document->doc->default_page_orientation;
372         }
373
374         if (rotation == GTK_GS_ORIENTATION_NONE)
375                 rotation = ps_document->doc->orientation;
376
377         if (rotation == GTK_GS_ORIENTATION_NONE)
378                 rotation = GTK_GS_ORIENTATION_PORTRAIT;
379
380         return rotation;
381 }
382
383 static void
384 ps_document_get_page_size (EvDocument *document,
385                            int         page,
386                            double     *width,
387                            double     *height)
388 {
389         PSDocument *ps_document = PS_DOCUMENT (document);
390         int urx, ury, llx, lly;
391         gdouble pwidth, pheight;
392         gdouble page_width, page_height;
393         gint rotate;
394
395         psgetpagebox (ps_document->doc, page, &urx, &ury, &llx, &lly);
396
397         pwidth = (urx - llx) + 0.5;
398         pheight = (ury - lly) + 0.5;
399
400         rotate = ps_document_get_page_rotation (ps_document, page);
401         if (rotate == 90 || rotate == 270) {
402                 page_height = pwidth;
403                 page_width = pheight;
404         } else {
405                 page_width = pwidth;
406                 page_height = pheight;
407         }
408         
409         if (width) {
410                 *width = page_width;
411         }
412
413         if (height) {
414                 *height = page_height;
415         }
416 }
417
418 static EvDocumentInfo *
419 ps_document_get_info (EvDocument *document)
420 {
421         EvDocumentInfo *info;
422         PSDocument *ps = PS_DOCUMENT (document);
423         int urx, ury, llx, lly;
424
425         info = g_new0 (EvDocumentInfo, 1);
426         info->fields_mask = EV_DOCUMENT_INFO_TITLE |
427                             EV_DOCUMENT_INFO_FORMAT |
428                             EV_DOCUMENT_INFO_CREATOR |
429                             EV_DOCUMENT_INFO_N_PAGES |
430                             EV_DOCUMENT_INFO_PAPER_SIZE;
431
432         info->title = g_strdup (ps->doc->title);
433         info->format = ps->doc->epsf ? g_strdup (_("Encapsulated PostScript"))
434                                      : g_strdup (_("PostScript"));
435         info->creator = g_strdup (ps->doc->creator);
436         info->n_pages = ev_document_get_n_pages (document);
437         
438         psgetpagebox (PS_DOCUMENT (document)->doc, 0, &urx, &ury, &llx, &lly);
439
440         info->paper_width  = (urx - llx) / 72.0f * 25.4f;
441         info->paper_height = (ury - lly) / 72.0f * 25.4f;
442
443         return info;
444 }
445
446 static void
447 ps_document_document_iface_init (EvDocumentIface *iface)
448 {
449         iface->load = ps_document_load;
450         iface->save = ps_document_save;
451         iface->get_n_pages = ps_document_get_n_pages;
452         iface->get_page_size = ps_document_get_page_size;
453         iface->get_info = ps_document_get_info;
454 }
455
456 /* EvAsyncRendererIface */
457 static void
458 ps_interpreter_page_rendered (PSInterpreter *gs,
459                               GdkPixbuf     *pixbuf,
460                               PSDocument    *ps_document)
461 {
462         g_signal_emit_by_name (ps_document, "render_finished", pixbuf);
463 }
464
465 static void
466 ps_async_renderer_render_pixbuf (EvAsyncRenderer *renderer,
467                                  gint             page,
468                                  gdouble          scale,
469                                  gint             rotation)
470 {
471         PSDocument *ps_document = PS_DOCUMENT (renderer);
472
473         g_return_if_fail (PS_IS_INTERPRETER (ps_document->gs));
474
475         rotation = (rotation + ps_document_get_page_rotation (ps_document, page)) % 360;
476
477         ps_interpreter_render_page (ps_document->gs, page, scale, rotation);
478 }
479
480 static void
481 ps_async_renderer_iface_init (EvAsyncRendererIface *iface)
482 {
483         iface->render_pixbuf = ps_async_renderer_render_pixbuf;
484 }
485
486 /* EvDocumentThumbnailsIface */
487 static void
488 ps_interpreter_thumbnail_rendered (PSInterpreter *gs,
489                                    GdkPixbuf     *pixbuf,
490                                    PSDocument    *ps_document)
491 {
492         if (ps_document->thumbnail)
493                 g_object_unref (ps_document->thumbnail);
494         ps_document->thumbnail = g_object_ref (pixbuf);
495
496         g_cond_broadcast (ps_document->thumbs_cond);
497 }
498
499 static gboolean
500 ps_document_render_thumbnail (PSDocument *ps_document)
501 {
502         ps_interpreter_render_page (ps_document->thumbs_gs,
503                                     ps_document->thumbs_rc->page,
504                                     ps_document->thumbs_rc->scale,
505                                     ps_document->thumbs_rc->rotation);
506
507         return FALSE;
508 }
509
510 static GdkPixbuf *
511 ps_document_thumbnails_get_thumbnail (EvDocumentThumbnails *document_thumbnails,
512                                       EvRenderContext      *rc, 
513                                       gboolean              border)
514 {
515         PSDocument *ps_document;
516         GdkPixbuf  *pixbuf = NULL;
517
518         ps_document = PS_DOCUMENT (document_thumbnails);
519         
520         g_return_val_if_fail (ps_document->filename != NULL, NULL);
521         g_return_val_if_fail (ps_document->doc != NULL, NULL);
522                 
523         if (!ps_document->thumbs_gs) {
524                 ps_document->thumbs_gs = ps_interpreter_new (ps_document->filename,
525                                                              ps_document->doc);
526                 g_signal_connect (G_OBJECT (ps_document->thumbs_gs), "page_rendered",
527                                   G_CALLBACK (ps_interpreter_thumbnail_rendered),
528                                   (gpointer) ps_document);
529         }
530
531         if (!ps_document->thumbs_mutex)
532                 ps_document->thumbs_mutex = g_mutex_new ();
533         ps_document->thumbs_cond = g_cond_new ();
534
535         if (ps_document->thumbs_rc)
536                 g_object_unref (ps_document->thumbs_rc);
537         ps_document->thumbs_rc = g_object_ref (rc);
538
539         ev_document_doc_mutex_unlock ();
540         g_mutex_lock (ps_document->thumbs_mutex);
541         g_idle_add ((GSourceFunc)ps_document_render_thumbnail, ps_document);
542         g_cond_wait (ps_document->thumbs_cond, ps_document->thumbs_mutex);
543         g_cond_free (ps_document->thumbs_cond);
544         ps_document->thumbs_cond = NULL;
545         g_mutex_unlock (ps_document->thumbs_mutex);
546         ev_document_doc_mutex_lock ();
547         
548         pixbuf = ps_document->thumbnail;
549         ps_document->thumbnail = NULL;
550         
551         if (border) {
552                 GdkPixbuf *border_pixbuf;
553                 
554                 border_pixbuf = ev_document_misc_get_thumbnail_frame (-1, -1, pixbuf);
555                 g_object_unref (pixbuf);
556                 pixbuf = border_pixbuf;
557         }
558
559         return pixbuf;
560 }
561
562 static void
563 ps_document_thumbnails_get_dimensions (EvDocumentThumbnails *document_thumbnails,
564                                        EvRenderContext      *rc, 
565                                        gint                 *width,
566                                        gint                 *height)
567 {
568         PSDocument *ps_document;
569         gdouble     page_width, page_height;
570
571         ps_document = PS_DOCUMENT (document_thumbnails);
572         
573         ps_document_get_page_size (EV_DOCUMENT (ps_document),
574                                    rc->page,
575                                    &page_width, &page_height);
576         
577         if (rc->rotation == 90 || rc->rotation == 270) {
578                 *width = (gint) (page_height * rc->scale);
579                 *height = (gint) (page_width * rc->scale);
580         } else {
581                 *width = (gint) (page_width * rc->scale);
582                 *height = (gint) (page_height * rc->scale);
583         }
584 }
585
586 static void
587 ps_document_document_thumbnails_iface_init (EvDocumentThumbnailsIface *iface)
588 {
589         iface->get_thumbnail = ps_document_thumbnails_get_thumbnail;
590         iface->get_dimensions = ps_document_thumbnails_get_dimensions;
591 }
592
593 /* EvFileExporterIface */
594 static void
595 ps_document_file_exporter_begin (EvFileExporter        *exporter,
596                                  EvFileExporterContext *fc)
597 {
598         PSDocument *document = PS_DOCUMENT (exporter);
599
600         if (document->structured_doc) {
601                 g_free (document->ps_export_pagelist);
602         
603                 document->ps_export_pagelist = g_new0 (int, document->doc->numpages);
604         }
605
606         document->ps_export_filename = g_strdup (fc->filename);
607 }
608
609 static void
610 ps_document_file_exporter_do_page (EvFileExporter  *exporter,
611                                    EvRenderContext *rc)
612 {
613         PSDocument *document = PS_DOCUMENT (exporter);
614         
615         if (document->structured_doc) {
616                 document->ps_export_pagelist[rc->page] = 1;
617         }
618 }
619
620 static void
621 ps_document_file_exporter_end (EvFileExporter *exporter)
622 {
623         PSDocument *document = PS_DOCUMENT (exporter);
624
625         if (!document->structured_doc) {
626                 save_document (document, document->ps_export_filename);
627         } else {
628                 save_page_list (document, document->ps_export_pagelist,
629                                 document->ps_export_filename);
630                 g_free (document->ps_export_pagelist);
631                 g_free (document->ps_export_filename);  
632                 document->ps_export_pagelist = NULL;
633                 document->ps_export_filename = NULL;
634         }
635 }
636
637 static EvFileExporterCapabilities
638 ps_document_file_exporter_get_capabilities (EvFileExporter *exporter)
639 {
640         return  EV_FILE_EXPORTER_CAN_PAGE_SET |
641                 EV_FILE_EXPORTER_CAN_GENERATE_PS;
642 }
643
644 static void
645 ps_document_file_exporter_iface_init (EvFileExporterIface *iface)
646 {
647         iface->begin = ps_document_file_exporter_begin;
648         iface->do_page = ps_document_file_exporter_do_page;
649         iface->end = ps_document_file_exporter_end;
650         iface->get_capabilities = ps_document_file_exporter_get_capabilities;
651 }